﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QUANLIHOCSINH.BussinessObject;
using QUANLIHOCSINH.BussinessLogic;

namespace QUANLIHOCSINH
{
    enum Status_QuanLiLopHoc { View, Insert, Update, Delete };
    public partial class FrmQuanLiLopHoc : Form
    {
        Status_QuanLiLopHoc TrangThai = Status_QuanLiLopHoc.View;
        LOPBUS lop = new LOPBUS();
        NAMHOCBUS namhoc = new NAMHOCBUS();
        KHOIBUS khoi = new KHOIBUS();
        NGUOIDUNGBUS nguoidung = new NGUOIDUNGBUS();
        public FrmQuanLiLopHoc()
        {
            InitializeComponent();
        }
        private void EnableButton(bool Enable)
        {
            btnThem.Visible = !Enable;
            btnSua.Visible = !Enable;
            btnXoa.Visible = !Enable;
            btnCapNhat.Visible = Enable;
            btnHuy.Visible = Enable;
            gblophoc.Enabled = Enable;
            
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            txtMaLop.Text = lop.NextID();
            EnableButton(true);
            btnThem.Visible = false;
           TrangThai = Status_QuanLiLopHoc.Insert;
        }
    
        private void btnCapNhat_Click(object sender, EventArgs e)
        {

            if (TrangThai == Status_QuanLiLopHoc.Insert)
            {
                if (cbGiaoVien.SelectedIndex >= 0)
                {
                    LOP l = new LOP();
                    l.MaLop = txtMaLop.Text;
                    l.MaNamHoc = cbNamHoc.SelectedValue.ToString();
                    l.MaKhoi = cbKhoi.SelectedValue.ToString();
                    l.MaGV = cbGiaoVien.SelectedValue.ToString();

                    l.TenLop = txtTenLop.Text;

                    if (lop.ThemLop(l))
                    {
                        FrmQuanLiLopHoc_Load(sender, e);
                    }
                }
                else
                {
                    MessageBox.Show("Khong Co Giao Vien Nao De Phan Cong");
                }
              
            }
            if (TrangThai == Status_QuanLiLopHoc.Update)
            {
                LOP l = new LOP();
                l.MaLop = txtMaLop.Text;
                l.MaNamHoc = cbNamHoc.SelectedValue.ToString();
                l.MaKhoi = cbKhoi.SelectedValue.ToString();
                l.TenLop = txtTenLop.Text;
                if (ckDoiGiaoVien.Enabled == true)
                {
                    if (cbGiaoVien.SelectedIndex >= 0)
                    {
                        l.MaGV = cbGiaoVien.SelectedValue.ToString();
                    }
                    else
                    {
                        l.MaGV = "";
                    }
                }
                else
                {
                    l.MaGV = "";
                }
                ckDoiGiaoVien.Checked = true;
                if (lop.SuaLop(l))
                {
                    FrmQuanLiLopHoc_Load(sender, e);
                }
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (dgvLop.Rows.Count > 1)
            {
                int r = dgvLop.CurrentRow.Index;
                txtTenLop.Text = dgvLop.Rows[r].Cells["TenLop"].Value.ToString();
                txtMaLop.Text = dgvLop.Rows[r].Cells["MaLop"].Value.ToString();
                cbKhoi.SelectedValue = dgvLop.Rows[r].Cells["ColKhoi"].Value.ToString();
            }
            cbGiaoVien.Enabled = false;
            ckDoiGiaoVien.Visible = true;
            EnableButton(true);
            btnSua.Visible = false;
            TrangThai = Status_QuanLiLopHoc.Update;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            ckDoiGiaoVien.Visible = false;
            EnableButton(false);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn Có Muốn Xóa", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                LOP l = new LOP();
                int r = dgvLop.CurrentCell.RowIndex;
                l.MaLop = dgvLop.Rows[r].Cells[1].Value.ToString();
    
                lop.XoaLop(l);
                EnableButton(false);
                FrmQuanLiLopHoc_Load(sender, e);
            }
        }

        private void FrmQuanLiLopHoc_Load(object sender, EventArgs e)
        {
            (dgvLop.Columns["ColGV"] as DataGridViewComboBoxColumn).DataSource = nguoidung.LayDanhSachGiaoVien();
            (dgvLop.Columns["ColGV"] as DataGridViewComboBoxColumn).DisplayMember = "TenNguoiDung";
            (dgvLop.Columns["ColGV"] as DataGridViewComboBoxColumn).ValueMember = "MaNguoiDung";

            (dgvLop.Columns["ColKhoi"] as DataGridViewComboBoxColumn).DataSource = khoi.LayDanhSachKhoi();
            (dgvLop.Columns["ColKhoi"] as DataGridViewComboBoxColumn).DisplayMember = "TenKhoi";
            (dgvLop.Columns["ColKhoi"] as DataGridViewComboBoxColumn).ValueMember = "MaKhoi";

            (dgvLop.Columns["ColNamHoc"] as DataGridViewComboBoxColumn).DataSource = namhoc.LayDanhSachNamHoc();
            (dgvLop.Columns["ColNamHoc"] as DataGridViewComboBoxColumn).DisplayMember = "TenNamHoc";
            (dgvLop.Columns["ColNamHoc"] as DataGridViewComboBoxColumn).ValueMember = "MaNamHoc";

            cbNamHoc.DataSource = namhoc.LayDanhSachNamHoc();
            cbNamHoc.DisplayMember = "TenNamHoc";
            cbNamHoc.ValueMember = "MaNamHoc";
            
            cbKhoi.DataSource = khoi.LayDanhSachKhoi();
            cbKhoi.DisplayMember = "TenKhoi";
            cbKhoi.ValueMember = "MaKhoi";

            cbGiaoVien.DataSource = nguoidung.LayDanhSachGiaoVienChuaPhanCong();
            cbGiaoVien.DisplayMember = "TenNguoiDung";
            cbGiaoVien.ValueMember = "MaNguoiDung";

            dgvLop.DataSource = lop.LayDanhSachLopHoc();
            EnableButton(false);
        }

        private void ckDoiGiaoVien_CheckedChanged(object sender, EventArgs e)
        {
            if (ckDoiGiaoVien.Checked == true)
            {
                cbGiaoVien.Enabled = true;
            }
            else
            {
                cbGiaoVien.Enabled = false;
            }
        }
    }
}
